﻿using Microsoft.EntityFrameworkCore;
using RescueApp.Data;
using RescueApp.Models;

namespace RescueApp.Data
{
    //DbContext 是 EF Core 中的核心类，用于管理数据库的连接和操作
    //RescueContext：定义一个数据库上下文，用于管理 RescueApply和 RescueDispatch两个实体类与数据库的交互。
    public class RescueContext : DbContext
    {
        private readonly string _connectionString;

        //RescueApplies 是一个属性，表示 RescueApply 实体类对应的数据库表。
        //通过 DbSet，可以对数据库表进行查询、插入、更新和删除操作。
        public DbSet<RescueApply> RescueApplies { get; set; }
       
        public DbSet<RescueDispatch> RescueDispatches { get; set; }

        public RescueContext() : base() { }
        public RescueContext(DbContextOptions<RescueContext> options) : base(options) { }

        public RescueContext(string connectionString)
        {
            _connectionString = connectionString;
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
            {
                optionsBuilder.UseSqlServer(_connectionString);////使用 SQL Server 数据库。
            }
        }

        protected override void OnModelCreating(ModelBuilder builder)
        {
            base.OnModelCreating(builder);

            builder.ApplyConfiguration(new RescueApplyConfig());
            builder.ApplyConfiguration(new RescueDispatchConfig());
        }
    }
}